package com.Lyoto.NetNotify.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Lyoto
 * @Description
 * @Date 2022-09-30 10:58
 * @Version
 **/
public class DateUtil {
    public static Date convertFirstTime(Date now, String rule) {
        String pratt = "yyyy-MM-dd HH:mm";
        String[] split = rule.split(":");
        String hour = split[0];
        String minutes = split[1];
        SimpleDateFormat dateFormat = new SimpleDateFormat(pratt);
        String nowString = dateFormat.format(now);
        String[] splitNow = nowString.split("-");

        String nowYear = splitNow[0];
        String nowMonth = splitNow[1];
        String dayAndTime = splitNow[2];
        String nowDay = dayAndTime.substring(0, dayAndTime.indexOf(" "));
        String[] hourAndMinute = dayAndTime.substring(dayAndTime.indexOf(" ") + 1).split(":");
        String nowHour = hourAndMinute[0];
        String nowMinutes = hourAndMinute[1];
        String patternDate = nowYear + "-" + nowMonth + "-" + nowDay + " " + hour + ":" + minutes;

        Date result = now;
        try {
            Date execDate = dateFormat.parse(patternDate);
            long dayTimeStamp = 86400000;
            while (now.getTime() >= execDate.getTime()) {
                execDate.setTime(execDate.getTime() + dayTimeStamp);
            }
            result = new Date(execDate.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 使用 yyyy-MM-dd hh:mm:ss.SSS 格式化时间为字符串
     * @param date
     * @return 格式化后的字符串时间
     */
    public static String dateDefaultFormat(Date date){
        return dataFormat(date,"yyyy-MM-dd hh:mm:ss.SSS");
    }

    private static String dataFormat(Date date,String pattern){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        return simpleDateFormat.format(date);
    }

    public static void main(String[] args) {
        Date date = DateUtil.convertFirstTime(new Date(), "21:30");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");

        System.out.println(format.format(date));
    }
}
